package com.cbtx.tablet.widget

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import androidx.appcompat.widget.AppCompatImageView
import com.cbtx.tablet.R

/**
 * author : joker.peng
 * e-mail : joker@fiture.com
 * date   : 2021/7/5 3:55 AM
 * desc   :
 */
class TabIndicatorView : RelativeLayout {

    private var mImgIcon: AppCompatImageView? = null


    var iconSelect = -1
    var title = ""
    var iconWith = 0
    var titleColor = -1
    private var mSelected = false

    constructor(context: Context) : this(context, null) {
        init()
    }

    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    constructor(context: Context, attributeSet: AttributeSet?, defStyle: Int) : super(context, attributeSet, defStyle) {
        val ta: TypedArray = context.obtainStyledAttributes(attributeSet, R.styleable.TabIndicatorView)
        iconSelect = ta.getResourceId(R.styleable.TabIndicatorView_tabIcon, -1)
        title = ta.getString(R.styleable.TabIndicatorView_tabTitle)!!
        titleColor = ta.getResourceId(R.styleable.TabIndicatorView_tabColor, -1)
        iconWith = ta.getResourceId(R.styleable.TabIndicatorView_tabIconWidth, 50)

        init()
    }

    /**
     * 初始化
     */
    private fun init() {
        View.inflate(context, R.layout.layout_tab_indicator, this)
        mImgIcon = findViewById(R.id.tab_img_icon)
        mImgIcon?.setImageDrawable(context.resources.getDrawable(iconSelect))
    }


    /**
     * 设置选中
     */
    fun setTabSelected(selected: Boolean) {
        mSelected = selected
        mImgIcon!!.isSelected = selected
    }

    /**
     * 获得选中项
     *
     * @return
     */
    fun isTabSelected(): Boolean {
        return mSelected
    }
}